﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
namespace BIN
{
    public class CheckService
    {
        public static void CheckSyncService()
        {
            CheckSvcRunning("YSSYNC");
        }
        public static void CheckSvcRunning(string svcName)
        {
            try
            {
                ServiceController sc = GetServiceControlCurr(svcName);
                if (null != sc)
                {
                    if (sc.Status != ServiceControllerStatus.Running)
                    {
                        StartService(svcName);
                    }
                }
            }
            catch (Exception ex)
            {
                AutoUpdateLog.LogObj.AddLog(LOG_MODE.ERROR, ex.Message);
            }
        }
        public static void RestartSyncService()
        {
            StopService("YSSYNC");
            StartService("YSSYNC");
        }
        private static ServiceController GetServiceControlCurr(string ServiceName)
        {
            ServiceController ctl = null;
            try
            {
                ctl = new ServiceController(ServiceName);
                string svcName = ctl.ServiceName;
            }
            catch
            {
                ctl = null;
            }
            return ctl;
        }
        private static void StopService(string serviceName)
        {
            System.ServiceProcess.ServiceController service =
                     GetServiceControlCurr(serviceName);
            if (null == serviceName) return;
            if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running ||
                service.Status == ServiceControllerStatus.StartPending)
            {
                service.Stop();
                for (int i = 0; i < 60; i++)
                {
                    service.Refresh();
                    System.Threading.Thread.Sleep(1000);
                    if (service.Status ==
                        System.ServiceProcess.ServiceControllerStatus.Stopped) break;
                    if (i == 59)
                    {
                        throw new Exception(
                            string.Format("停止服务[{0}]失败!", serviceName)
                            );
                    }
                }
            }
        }
        private static void StartService(string serviceName)
        {
            System.ServiceProcess.ServiceController service =
               GetServiceControlCurr(serviceName);
            if (null == service) return;
            if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running
                && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
            {
                service.Start();
                for (int i = 0; i < 60; i++)
                {
                    service.Refresh();
                    System.Threading.Thread.Sleep(1000);
                    if (service.Status ==
                        System.ServiceProcess.ServiceControllerStatus.Running) break;
                    if (i == 59)
                    {
                        throw new Exception(
                          string.Format(
                          "启动服务[{0}]失败!", serviceName));
                    }
                }
            }
        }
    }
}
